﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;

namespace AF_Web
{
    public class DatatableAjax
    {
        private static DatatableAjax _instance = null;
        public static DatatableAjax Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DatatableAjax();
                }
                return _instance;
            }
        }
        public string ConvertToDTJson<T>(string draw, List<T> Data, int allCount) where T : class
        {
            if (string.IsNullOrEmpty(draw))
            {
                draw = "1";
            }
            StringBuilder SB = new StringBuilder();
            if (Data != null)
            {
                string JsonStr = GetDataJson(Data);
                return "{\"draw\": " + draw + ",\"recordsTotal\": " + allCount + ",\"recordsFiltered\": " + allCount + ",\"data\":" + JsonStr + "}";
            }
            else
            {
                return "{\"draw\": " + draw + ",\"recordsTotal\": 0,\"recordsFiltered\": 0,\"data\":[]}";
            }
        }

        public string GetDataJson<T>(T Data)
        {
            Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm";
            return Newtonsoft.Json.JsonConvert.SerializeObject(Data, timeConverter);
        }
    }
}